package com.ruoyi.ycl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ycl.domain.bo.BsSupBo;
import com.ruoyi.ycl.domain.vo.BsSupVo;
import com.ruoyi.ycl.domain.BsSup;
import com.ruoyi.ycl.mapper.BsSupMapper;
import com.ruoyi.ycl.service.IBsSupService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 供应商Service业务层处理
 *
 * @author wdd
 * @date 2024-04-30
 */
@RequiredArgsConstructor
@Service
public class BsSupServiceImpl implements IBsSupService {

    private final BsSupMapper baseMapper;

    /**
     * 查询供应商
     */
    @Override
    public BsSupVo queryById(Long supId){
        return baseMapper.selectVoById(supId);
    }

    /**
     * 查询供应商列表
     */
    @Override
    public TableDataInfo<BsSupVo> queryPageList(BsSupBo bo, PageQuery pageQuery) {
        // 只能查自己试验室的 供应商
        bo.setDeptId(LoginHelper.getDeptId());
        LambdaQueryWrapper<BsSup> lqw = buildQueryWrapper(bo);
        Page<BsSupVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询供应商列表
     */
    @Override
    public List<BsSupVo> queryList(BsSupBo bo) {
        // 只能查自己试验室的 供应商
        bo.setDeptId(LoginHelper.getDeptId());
        LambdaQueryWrapper<BsSup> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BsSup> buildQueryWrapper(BsSupBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsSup> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjUtil.isNotNull(bo.getProjectId()), BsSup::getProjectId, bo.getProjectId());
        lqw.eq(ObjUtil.isNotNull(bo.getDeptId()), BsSup::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getSupName()), BsSup::getSupName, bo.getSupName());
        lqw.eq(StringUtils.isNotBlank(bo.getClType()), BsSup::getClType, bo.getClType());
        lqw.like(StringUtils.isNotBlank(bo.getClName()), BsSup::getClName, bo.getClName());
        lqw.eq(StringUtils.isNotBlank(bo.getClCode()), BsSup::getClCode, bo.getClCode());
        lqw.eq(StringUtils.isNotBlank(bo.getLianxr()), BsSup::getLianxr, bo.getLianxr());
        lqw.eq(StringUtils.isNotBlank(bo.getGongyfw()), BsSup::getGongyfw, bo.getGongyfw());
        lqw.eq(StringUtils.isNotBlank(bo.getLianxdh()), BsSup::getLianxdh, bo.getLianxdh());
        return lqw;
    }

    /**
     * 新增供应商
     */
    @Override
    public Boolean insertByBo(BsSupBo bo) {
        // 只能维护自己试验室的 供应商
        bo.setDeptId(LoginHelper.getDeptId());
        BsSup add = BeanUtil.toBean(bo, BsSup.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSupId(add.getSupId());
        }
        return flag;
    }

    /**
     * 修改供应商
     */
    @Override
    public Boolean updateByBo(BsSupBo bo) {
        BsSup update = BeanUtil.toBean(bo, BsSup.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BsSup entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除供应商
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
